{% assign maxRelated = 4 %}

{% assign minCommonTags =  2 %}

{% assign maxRelatedCounter = 0 %}
<div class="related-post-container row">
    {% for post in site.posts %}

        {% assign sameTagCount = 0 %}
        {% assign commonTags = '' %}

        {% for tag in post.tags %}

        {% if post.url != page.url %}
            {% if page.tags contains tag %}
            {% assign sameTagCount = sameTagCount | plus: 1 %}
            {% capture tagmarkup %} <span class="label label-default">{{ tag }}</span> {% endcapture %}
            {% assign commonTags = commonTags | append: tagmarkup %}
            {% endif %}
        {% endif %}
        {% endfor %}

        {% if sameTagCount >= minCommonTags %}
            <div class="post-detail col-xs-12 col-sm-6">
                {% if page.featured-image %}
                    {% include post-featured-image.html image=page.featured-image alt=page.featured-image-alt %}
                {% endif %}
                <h5>
                    <a href="{{ site.baseurl }}{{ post.url }}">{{ post.title }}</a>
                </h5>
                <p class="post-meta">
                    {%- if page.author -%}
                        <span itemprop="author" itemscope itemtype="http://schema.org/Person"><span class="p-author h-card"
                            itemprop="name">{{ page.author | escape }}</span></span>
                    {%- endif -%}
                    <time class="dt-published" datetime="{{ page.date | date_to_xmlschema }}" itemprop="datePublished">
                        {%- assign date_format = site.minima.date_format | default: "%b %-d, %Y" -%}
                        {{ page.date | date: date_format }}
                    </time>
                </p>    
            </div>
        {% assign maxRelatedCounter = maxRelatedCounter | plus: 1 %}
        {% if maxRelatedCounter >= maxRelated %}
            {% break %}
        {% endif %}
        {% endif %}

    {% endfor %}
</div>